package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/19
 * @author: gyhe10
 * @description: 轮播图业务控制层
 **/

@Api(tags = "轮播图接口管理")
@RestController
@RequestMapping("admin/indexImg")
public class IndexImgController {

    @Resource
    private IndexImgService indexImgService;

    /**
     * 多条件分页查询轮播图
     * @param current   页码
     * @param size      每页记录数
     * @param status    状态
     * @return
     */
    @ApiOperation("多条件分页查询轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) Integer status) {
        Page<IndexImg> page = new Page<>(current, size);
        page = indexImgService.page(page, new LambdaQueryWrapper<IndexImg>()
                .eq(ObjectUtil.isNotNull(status), IndexImg::getStatus, status)
                .orderByDesc(IndexImg::getSeq));
        return Result.success(page);
    }

    /**
     * 新增轮播图
     * @param indexImg 轮播图对象
     * @return
     */
    @ApiOperation("新增轮播图")
    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@RequestBody IndexImg indexImg) {
        Boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handle(saved);
    }

    /**
     * 根据ID查询轮播图详情
     * @param indexImgId 轮播图ID
     * @return
     */
    @ApiOperation("根据ID查询轮播图详情")
    @GetMapping("info/{indexImgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> loadIndexImgInfo(@PathVariable Long indexImgId) {
        IndexImg indexImg = indexImgService.queryIndexImgInfoById(indexImgId);
        return Result.success(indexImg);
    }

    /**
     * 修改轮播图
     * @param indexImg 轮播图对象
     * @return
     */
    @ApiOperation("修改轮播图")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@RequestBody IndexImg indexImg) {
        Boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }

    /**
     * 单个/批量删除轮播图
     * @param indexImgIds
     * @return
     */
    @ApiOperation("单个/批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@RequestBody List<Long> indexImgIds) {
        Boolean removed = indexImgService.removeIndexImg(indexImgIds);
        return Result.handle(removed);
    }


    ///////////////// 微信小程序接口 ////////////////

    /**
     * 查询小程序轮播图列表
     * @return
     */
    @ApiOperation("查询小程序轮播图列表")
    @GetMapping("indexImgs")
    public Result<List<IndexImg>> loadWxIndexImgList() {
        List<IndexImg> list = indexImgService.queryIndexImgList();
        return Result.success(list);
    }
}
